﻿using PTop.Code;
using PTop.Domain.Entity.Biz;
using PTop.Domain.IRepository.Biz;
using PTop.Repository.Biz;
using System.Collections.Generic;

namespace PTop.Application.Biz
{
    /// <summary>
    /// 工作应聘App
    /// </summary>	
    public class JobCandidateApp
    {
        private IJobCandidateRepository service = new JobCandidateRepository();

        public List<JobCandidateEntity> GetList(Pagination pagination, string pid, string keyword, int state)
        {
            var expression = ExtLinq.True<JobCandidateEntity>();
            if (!string.IsNullOrEmpty(pid))
            {
                expression = expression.And(t => t.F_JobId == pid);
            }
            if (!string.IsNullOrEmpty(keyword))
            {
                expression = expression.And(t => t.F_JobTitle.Contains(keyword));
                expression = expression.Or(t => t.F_Name.Contains(keyword));
                expression = expression.Or(t => t.F_Phone.Contains(keyword));
            }
            if (state > -1)
            {
                expression = expression.And(t => t.F_State == state);
            }
            return service.FindList(expression, pagination);
        }

        public JobCandidateEntity GetForm(string keyValue)
        {
            return service.FindEntity(keyValue);
        }
        public JobCandidateEntity GetForm(string openId, string jobId)
        {
            return service.FindEntity(m => m.F_CreatorUserId == openId && m.F_JobId == jobId);
        }
        public void Delete(string keyValue)
        {
            service.Delete(t => t.F_Id == keyValue);
        }
        public void SubmitForm(JobCandidateEntity entity, string keyValue)
        {
            if (!string.IsNullOrEmpty(keyValue))
            {
                entity.Modify(keyValue);
                service.Update(entity);
                new UserStatisticsApp().AddCount("F_JobDealCount");
            }
            else
            {
                entity.Create();
                //entity.F_LastModifyTime = entity.F_CreatorTime;
                //entity.F_LastModifyUserId = entity.F_CreatorUserId;
                service.Insert(entity);
            }
        }

        #region For Front

        public bool Update(JobCandidateEntity entity, string keyValue, out string msg)
        {
            if (!string.IsNullOrEmpty(keyValue))
            {
                var old = service.FindEntityWithNoTracking(m => m.F_Id == keyValue);
                if (old == null)
                {
                    msg = "实体不存在";
                    return false;
                }
                if (old.F_State > 0)
                {
                    msg = "管理员已经处理，不可修改";
                    return false;
                }
                service.Update(entity);
            }
            SubmitForm(entity, keyValue);
            msg = "SUCCESS";
            return true;
        }

        #endregion
    }
}